package com.zj.mq.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {
    private String QUEUE_NAME = "queue.order";
    private String EX_NAME = "ex.order";
    private String ROUTING_KEY = "key.order";

    @Bean
    public Queue queue() {
        Map<String,Object> props = new HashMap<>();

        props.put("x-message-ttl",10*1000);
        props.put("x-dead-letter-exchange",EX_NAME+".dlx");
        props.put("x-dead-letter-routing-key",ROUTING_KEY+".dlx");
        return new Queue(QUEUE_NAME, false, false, false, props);
    }

    @Bean
    public Exchange exchange() {
        return new DirectExchange(EX_NAME, false, false, null);
    }

    @Bean
    public Binding binding() {
        return new Binding(QUEUE_NAME,
                Binding.DestinationType.QUEUE,
                EX_NAME,
                ROUTING_KEY,
                null);
    }

    @Bean
    public Queue queueDlx() {
        return new Queue(QUEUE_NAME + ".dlx", false, false, false, null);
    }

    @Bean
    public Exchange exchangeDlx() {
        return new DirectExchange(EX_NAME + ".dlx", false, false, null);
    }

    @Bean
    public Binding bindingDlx() {
        return new Binding(QUEUE_NAME + ".dlx",
                Binding.DestinationType.QUEUE,
                EX_NAME + ".dlx",
                ROUTING_KEY + ".dlx",
                null);
    }
}
